package com.manage.web.controller;

import com.manage.dao.IEmpDao;
import com.manage.domain.Clazz;
import com.manage.service.IClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDate;
import java.util.List;

@Controller
@RequestMapping("/clazz")
public class ClazzController {

    @Autowired
    private IClazzService clazzService;
    @Autowired
    private IEmpDao empDao;

    @RequestMapping("/list.do")
    public String list(Model model) {
        List<Clazz> list = clazzService.list();
        model.addAttribute("list", list);
        model.addAttribute("empList", empDao.list());
        return "clazz-list";
    }

    @RequestMapping(value = "/add.do", method = RequestMethod.POST)
    public String add(Clazz clazz, 
                     @RequestParam(required = false, name = "beginDateStr") String beginDate,
                     @RequestParam(required = false, name = "endDateStr") String endDate,
                     Model model) {
        try {
            System.out.println("接收到的班级数据: " + clazz.getName() + ", " + clazz.getRoom() + ", " + clazz.getMasterId() + ", " + clazz.getSubject());
            // 手动处理日期转换
            if (beginDate != null && !beginDate.trim().isEmpty()) {
                clazz.setBeginDate(LocalDate.parse(beginDate));
            }
            if (endDate != null && !endDate.trim().isEmpty()) {
                clazz.setEndDate(LocalDate.parse(endDate));
            }
            clazzService.add(clazz);
            return "redirect:/clazz/list.do";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", "新增班级失败: " + e.getMessage());
            return list(model);
        }
    }

    @RequestMapping("/update.do")
    public String update(Clazz clazz, 
                        @RequestParam(required = false, name = "beginDateStr") String beginDate,
                        @RequestParam(required = false, name = "endDateStr") String endDate,
                        Model model) {
        try {
            // 手动处理日期转换
            if (beginDate != null && !beginDate.trim().isEmpty()) {
                clazz.setBeginDate(LocalDate.parse(beginDate));
            }
            if (endDate != null && !endDate.trim().isEmpty()) {
                clazz.setEndDate(LocalDate.parse(endDate));
            }
            clazzService.update(clazz);
            return "redirect:/clazz/list.do";
        } catch (Exception e) {
            model.addAttribute("error", e.getMessage());
            return list(model);
        }
    }

    @RequestMapping("/delete.do")
    public String delete(@RequestParam Integer id, Model model) {
        try {
            clazzService.delete(id);
            return "redirect:/clazz/list.do";
        } catch (Exception e) {
            model.addAttribute("error", e.getMessage());
            return list(model);
        }
    }
}


